package com.github.calvin.view;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.github.calvin.R;
import com.github.calvin.service.MMService;

public class ServiceActivity extends Activity implements View.OnClickListener {
    private Button btnStart, btnStop, btnBind, btnUnBind, btnGet;
    private Intent serviceIntent;
    private MMService.MMServiceBinder mmServiceBinder;
    private MMService mmService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);
        btnStart = (Button) this.findViewById(R.id.btnStart);
        btnStop = (Button) this.findViewById(R.id.btnStop);
        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);

        btnBind = (Button) this.findViewById(R.id.btnBind);
        btnUnBind = (Button) this.findViewById(R.id.btnUnBind);
        btnBind.setOnClickListener(this);
        btnUnBind.setOnClickListener(this);

        btnGet = (Button) this.findViewById(R.id.btnGet);
        btnGet.setOnClickListener(this);

        serviceIntent = new Intent(ServiceActivity.this, MMService.class);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnStart:
                startService(serviceIntent);
                Toast.makeText(getApplicationContext(), "Start Service", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btnStop:
                stopService(serviceIntent);
                Toast.makeText(getApplicationContext(), "Stop Service", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btnBind:
                bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
                Toast.makeText(getApplicationContext(), "Bind Service", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btnUnBind:
                unbindService(connection);
                Toast.makeText(getApplicationContext(), "UnBind Service", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btnGet:
                System.out.println("Current Num :" + mmService.getCurrentNum());
                break;
        }
    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            System.out.println("---onServiceConnected");
            mmServiceBinder = (MMService.MMServiceBinder) service;
            mmService = mmServiceBinder.getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            System.out.println("---onServiceDisconnected");
        }
    };
}
